﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace TIMA.DevTeam
{
    public class Paranavision
    {
        public class addr
        {
            public int adr = 0;
            public int ind = 0;

            public addr(int a, int b)
            {
                adr = a;
                ind = b;
            }
        } 

        private double Euclidian(double[] a, double[] b)
        {
            double x = 0;
            for (int i = 0; i < a.Length; i++)
            {
                x = x + Math.Pow((a[i] - b[i]), 2);
            }
            x = Math.Sqrt(x);
            return x;
        }

        public Dictionary<int, double> GetSimilarsTopN(double[] FeatureVector, Dictionary<int, double[]> DatabaseFeatures, int Threshold, List<double> Weights = null)
        {
            int[] DatabaseIDs = DatabaseFeatures.Keys.ToArray();
            double[][] DatabaseFeatures2 = DatabaseFeatures.Values.ToArray();



            double[] histol = new double[10];
            double[] histoa = new double[10];
            double[] histob = new double[10];
            double res;
            double rap = 0;

            double[] histol2 = new double[10];
            double[] histoa2 = new double[10];
            double[] histob2 = new double[10];
            double res2;

            addr[] add = new addr[DatabaseFeatures2.Length];

            double[] dist = new double[DatabaseFeatures2.Length];

            Dictionary<int, double> result = new Dictionary<int, double>();
            double[] FeatureVector2 = new double[FeatureVector.Length];
            for (int k = 0; k < 10; k++)
            {
                histol[k] = FeatureVector[k + 1];
                histoa[k] = FeatureVector[k + 11];
                histob[k] = FeatureVector[k + 21];
            }
            res = FeatureVector[0];
            rap = FeatureVector[46];
            for (int j = 0; j < DatabaseFeatures2.Length; j++)
            {
                res2 = DatabaseFeatures2[j][0];
                if (res == res2)
                {

                    for (int k = 0; k < 10; k++)
                    {
                        histol2[k] = DatabaseFeatures2[j][k + 1];
                        histoa2[k] = DatabaseFeatures2[j][k + 11];
                        histob2[k] = DatabaseFeatures2[j][k + 21];

                    }


                    dist[j] = Euclidian(histol, histol2) + Euclidian(histoa, histoa2) + Euclidian(histob, histob2);

                }
                else
                {
                    dist[j] = 10000000000000;

                }
                add[j] = new addr(DatabaseIDs[j], j);

            }
            Array.Sort(dist, add);
            int far = 0;
            for (int i = 0; i < 5; i++)
            {
                if (dist[i] >= 1)
                {
                    far = 1;
                }
            }
            if (far == 0)
            {
                if (rap == 0)
                {
                    for (int i = 0; i < Threshold/*100*/; i++)
                    {
                        result.Add(add[i].adr, dist[i]);
                    }
                }
                else
                {
                    addr[] add2 = new addr[50];
                    double[] dist2 = new double[50];
                    add2[0] = add[0];
                    dist2[0] = -1;
                    for (int i = 1; i < 50; i++)
                    {
                        add2[i] = add[i];
                        dist2[i] = Math.Abs(rap - DatabaseFeatures2[add2[i].ind][46]);
                    }
                    Array.Sort(dist2, add2);
                    for (int i = 0; i < Threshold/*50*/; i++)
                    {
                        result.Add(add2[i].adr, dist2[i]);
                    }
                }

            }
            else
            {
                result = GetSimilarsTopNmam(FeatureVector, DatabaseFeatures, Threshold);
            }

            return result;
        }

        private Dictionary<int, double> GetSimilarsTopNmam(double[] FeatureVector, Dictionary<int, double[]> DatabaseFeatures, int Threshold, List<double> Weights = null)
        {
            int[] DatabaseIDs = DatabaseFeatures.Keys.ToArray();
            double[][] DatabaseFeatures2 = DatabaseFeatures.Values.ToArray();

            double[] histo = new double[15];
            double res;
            double rap = 0;

            double[] histo2 = new double[15];
            double res2;

            addr[] add = new addr[DatabaseFeatures2.Length];

            double[] dist = new double[DatabaseFeatures2.Length];
            Dictionary<int, double> result = new Dictionary<int, double>();
            double[] FeatureVector2 = new double[FeatureVector.Length];
            for (int k = 0; k < 15; k++)
            {
                histo[k] = FeatureVector[k + 31];

            }
            res = FeatureVector[0];
            rap = FeatureVector[46];

            for (int j = 0; j < DatabaseFeatures2.Length; j++)
            {
                res2 = DatabaseFeatures2[j][0];
                if (res == res2)
                {

                    for (int k = 0; k < 15; k++)
                    {
                        histo2[k] = DatabaseFeatures2[j][k + 31];

                    }

                    dist[j] = Euclidian(histo, histo2);
                }
                else
                {
                    dist[j] = 10000000000000;
                }
                add[j] = new addr(DatabaseIDs[j], j);

            }
            Array.Sort(dist, add);
            if (rap == 0)
            {
                for (int i = 0; i < Threshold/*100*/; i++)
                {
                    result.Add(add[i].adr, dist[i]);
                }
            }
            else
            {

                addr[] add2 = new addr[50];
                double[] dist2 = new double[50];
                add2[0] = add[0];
                dist2[0] = -1;
                for (int i = 1; i < 50; i++)
                {
                    add2[i] = add[i];
                    dist2[i] = Math.Abs(rap - DatabaseFeatures2[add2[i].ind][46]);
                }
                Array.Sort(dist2, add2);
                for (int i = 0; i < Threshold/*50*/; i++)
                {
                    result.Add(add2[i].adr, dist2[i]);
                }
            }
            return result;
        }


    }
}
